<template>
  <page-layout>
    <div slot="toolbar">
      <el-button size="small" round type="primary" @click="newContent">新增活动</el-button>
    </div>
    <div class="card-header">活动管理</div>
    <el-card class="box-card ">
      <el-card class="box-card">
      <el-row>
        <el-col :span="24">
          <el-form ref="searchContentCondition" :model="searchContentCondition" label-width="80px" inline>
            <el-form-item label="标题">
              <el-input v-model="searchContentCondition.title" style="width: 300px;"/>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="searchContentCondition.status" placeholder="请选择">
                <el-option
                  v-for="item in ContentStatus"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="search(1)">查询</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      </el-card>
      <el-card class="box-card">
            <el-table
              :data="contentList.content" border stripe
              style="width: 100%">
              <el-table-column
                prop="id"
                label="编号"
                width="55">
              </el-table-column>
              <el-table-column prop="cover" label="封面图" width="110">
                <template slot-scope="scope">
                  <image-cover v-if="scope.row.cover" :thumb.sync="scope.row.cover" width="63px" height="63px" size-type="contain"></image-cover>
                  <span v-else>暂时无图</span>
                </template>
              </el-table-column>
              <el-table-column prop="title" label="标题" ></el-table-column>
              <el-table-column
                prop="startTime"
                label="开始时间" >
              </el-table-column>
              <el-table-column
                prop="endTime"
                label="结束时间">
              </el-table-column>
              <el-table-column
                prop="status"
                label="状态" width="120">
                <template slot-scope="scope">
                   <span>{{scope.row.status| keyText('ContentStatus')}}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="120">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" @click="preContent(scope.row)">
                    预览
                  </el-button>
                  <el-button size="mini"  @click="editContent(scope.$index, scope.row)" type="text">编辑</el-button>
                  <el-button size="mini" type="text" @click="deleteContent(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="mt10">
              <el-pagination
                background
                layout="prev, pager, next"
                :total="contentList.totalElements"
                :page-size="contentList.size"
                :current-page.sync="currentContentPage" @current-change="getPageChange">
              </el-pagination>
            </div>
          </el-card>
    </el-card>
    <el-dialog :visible.sync="showContentEditor" title="添加/编辑 活动" width="80%">
      <content-editor @close="onContentEditorClose" :is-close="showContentEditor"></content-editor>
    </el-dialog>
    <el-dialog title="预览" :visible.sync="showPreview" width="50%">
      <pre-content @close="onShowPreviewClose" :is-close="showPreview"></pre-content>
    </el-dialog>


  </page-layout>
</template>
<script>
import PageLayout from "@/components/page-layout";
import ContentEditor from "./activity-editor";
import { mapGetters } from "vuex";
import ImageCover from "@/components/image-cover";
import PreContent from "./pre-activity";
import Status from "@/libs/status";
import _ from "lodash";
export default {
  created() {
    this.getPageChange(1);
  },
  mounted() {
    this.ContentStatus = _.clone(Status.ContentStatus);
    this.ContentStatus.unshift({ value: "", text: "全部" });
  },
  data() {
    return {
      currentContentPage: 1,
      showContentEditor: false,
      showPreview: false,
      curPreObj: {},
      ContentStatus: []
    };
  },
  computed: {
    ...mapGetters({
      contentList: "contentList",
      searchContentCondition: "searchContentCondition"
    })
  },
  methods: {
    getPageChange(val) {
      console.log(this.searchContentCondition);
      var tem = Object.assign(
        { pageIndex: val - 1 },
        this.searchContentCondition
      );
      this.$store.dispatch("getContentPageS", tem).then(ret => {});
    },
    search(val) {
      this.getPageChange(1);
    },
    deleteContent(row) {
      this.$confirm("确认删除当前活动?", "提示", {})
        .then(() => {
          this.$store.dispatch("deleteContentS", { id: row.id }).then(ret => {
            this.getPageChange(this.currentContentPage);
            this.$notify({
              title: "删除成功",
              type: "success"
            });
          });
        })
        .catch(() => {});
    },
    preContent(row) {
      this.$store.dispatch("getContentS", row.id);
      this.showPreview = true;
    },
    newContent() {
      this.$store.commit("INIT_CURRENT_ACTIVITY");
      this.showContentEditor = true;
    },
    editContent(index, row) {
      this.$store.dispatch("getContentS", row.id);
      this.showContentEditor = true;
    },
    onContentEditorClose() {
      this.showContentEditor = false;
      this.getPageChange(this.currentContentPage);
    },
    save(row, status) {
      row.opt = status;
      this.$store.dispatch("addOrUpdateContentS", row).then(ret => {
        this.$notify({
          title: "提交成功",
          type: "success"
        });
        this.getPageChange(1);
      });
    },
    onShowPreviewClose() {
      this.showPreview = false;
      this.getPageChange(this.currentContentPage);
    }
  },
  components: {
    PageLayout,
    ContentEditor,
    ImageCover,
    PreContent
  }
};
</script>
<style>
</style>
